<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html dir="ltr"><head>
  <meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
  <title>COM tutorial</title><xml></xml>
  <link rel="stylesheet" type="text/css" href="MSDN.css"></head>
<body id="divID" class="dtBODY">
<div id="nsbanner">
<div id="bannerrow1">
<table class="bannerparthead" cellspacing="0">
  <tbody>
    <tr id="hdr">
      <td class="runninghead">
      <p align="left">CS-Script 3.27.0</p>
      </td>
      <td class="product"> </td>
    </tr>
  </tbody>
</table>
</div>
<div id="TitleRow">
<h1 class="dtH1" align="left">Using COM</h1>
</div>
</div>
<div id="nstext">
<h4><span class="dtH4">Step-by-step tutorial</span></h4>
The following tutorial shows how to call the method of a COM object
from the script.
<p>Thre are two possibe approaches to access COM objects:
importing type libraries manually or simplified "<span style="font-style: italic;">Single-line COM access</span>"
approach. It is recomended to use&nbsp;"<span style="font-style: italic;">Single-line COM access</span>"
as it&nbsp;provides seamless access to COM (no manual steps are
required). It also allows referencing the COM objects by GUIDS, ProgID
or
file name (.dll/.ocx) directly from the code.&nbsp;</p>
<p style="font-style: italic;">Note: the location of the&nbsp;generated Runtime Collable Wrapper file depends on the HideAutoGeneratedFiles setting value (see <a href="Config.html">configuration console</a> for details). The location of this file does not affect the script code in any way.</p>
<p></p>
<p style="font-weight: bold; font-style: italic;">"Single-line
COM access" </p>
Download tutorial source code:&nbsp;<a href="Tutorial/interop/interop.zip"><span style="font-style: italic;">interop.zip</span></a>&nbsp;
<ol>
  <li>Extract<span style="font-style: italic;">
ATLSimpleServ.dll</span>&nbsp;from the zip file.<br>
This file contains implementation of <span style="color: rgb(51, 0, 153);">CTestObjClass</span>
CoClass. This class
has only one method <span style="color: rgb(51, 0, 153);">Test()</span>
that displays the message
box.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</li>
  <li>Create file <span style="font-style: italic;">Interop.cs</span>&nbsp;<span style="font-style: italic;"></span>that contains the
following code:&nbsp;<br>
    <table style="width: 710px; text-align: left;" border="1" cellpadding="2" cellspacing="2">
      <tbody>
        <tr>
          <td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font face="Courier New" size="2"><font color="#008000" size="2">//css_prescript&nbsp;com(ATLSimpleServ.dll,&nbsp;SimpleServ,&nbsp;/r);<br>
          </font></font><font face="Courier New" size="2"><font color="#0000ff" size="2">using</font>&nbsp;System;<br>
          <font color="#0000ff" size="2">using</font>&nbsp;SimpleServ;</font><br>
          <font face="Courier New" size="2"><br>
          <font color="#0000ff" size="2">class</font>&nbsp;Test<br>
{<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">public</font>&nbsp;<font color="#0000ff" size="2">static</font>&nbsp;<font color="#0000ff" size="2">void</font>&nbsp;Main(<font color="#0000ff" size="2">string</font>[]&nbsp;args)<br>
&nbsp;&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CTestObjClass&nbsp;COMObject&nbsp;=&nbsp;<font color="#0000ff" size="2">new</font>&nbsp;CTestObjClass();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;COMObject.Test();<br>
&nbsp;&nbsp;&nbsp;&nbsp;}<br>
}</font></td>
        </tr>
      </tbody>
    </table>
Make sure <span style="font-style: italic;">Interop.cs</span>
is&nbsp;in the same directory where <span style="font-style: italic;">ATLSimpleServ.dll </span>is
located.</li>
  <li>Execute the following command in command prompt:<br>
    <div class="syntax">cscs interop </div>
  </li>
</ol>
<h4 class="dtH4">
Output&nbsp;</h4>
<p>The script will display "CTestObj::Test" MessageBox.</p>
<p style="text-align: center;"><img style="width: 532px; height: 151px;" alt="" src="Images/interop_tut.PNG"><br>
</p>
<p> </p>
<h4 class="dtH4">Code discussion</h4>
<p><span style="font-style: italic;">The
"Single-line COM access" is heavily based on the concept of&nbsp;</span><a style="font-style: italic;" href="pre_post_scripts.html">Pre-
and Post-execution scripts</a><span style="font-style: italic;">.<br>
</span></p>
<p>In this example the script creates the instance and calls
the method <span style="color: rgb(51, 0, 153);">Test()</span>
of the COM object <span style="color: rgb(51, 0, 153);">CTestObj</span>.&nbsp;</p>
<p>At the start of the execution the script engine executes <span style="font-style: italic;">com.cs</span> script
specified with the pre-script directive<span style="color: rgb(51, 0, 153);"></span>:&nbsp;</p>
<table style="width: 710px; text-align: left;" border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font face="Courier New" size="2"><font color="#008000" size="2">//css_prescript&nbsp;com(ATLSimpleServ.dll,&nbsp;SimpleServ,&nbsp;/r);</font></font></td>
    </tr>
  </tbody>
</table>
It instructs the <span style="font-style: italic;">com.cs</span>
script to register the COM Server&nbsp;implemented in&nbsp;<span style="font-style: italic;">ATLSimpleServ.dll and </span>to<span style="font-style: italic;"> </span>import
it's<span style="font-style: italic;"> </span>type
library into&nbsp;<span style="color: rgb(0, 0, 0); font-style: italic;">SimpleServ.dll
</span><span style="color: rgb(0, 0, 0);">assembly
(containing proxy</span> class for the&nbsp;<span style="color: rgb(51, 0, 153);">CTestObj</span>).<br>
If you wish you may instruct the script engine to unregister <span style="font-style: italic;">ATLSimpleServ.dll</span>&nbsp;after
the execution:
<table style="width: 710px; text-align: left;" border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font face="Courier New" size="2"><font color="#008000" size="2">//css_prescript&nbsp;com(ATLSimpleServ.dll,&nbsp;SimpleServ,&nbsp;/r);<br>
      </font><font color="#008000" size="2">//css_postscript&nbsp;com(ATLSimpleServ.dll,&nbsp;/u);</font></font></td>
    </tr>
  </tbody>
</table>
<p>
The <span style="font-style: italic;">com.cs</span>
script allows
importing the COM servers specified not only by the name of the dll
implementing it, but also by GUID or ProgID which can be used to
&nbsp;identify the corresponding type library.&nbsp;</p>
<p>This is an example of accessing the COM server&nbsp;<span style="color: rgb(51, 0, 153);">SYSINFO.SysInfo.1</span>
in
order to retreive battery status (<a href="Tutorial/interop/SysInfo.cs"><span style="font-style: italic;">SysInfo.cs</span></a>):
</p>
<table style="width: 710px; text-align: left;" border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font face="Courier New" size="2"><font color="#008000" size="2">//css_prescript&nbsp;com(SYSINFO.SysInfo.1,&nbsp;SisInfoLib);</font></font><font face="Courier New" size="2"><font color="#0000ff" size="2"><br>
using</font>&nbsp;System;<br>
      <font color="#0000ff" size="2">using</font>&nbsp;SisInfoLib;<br>
      <br>
      <font color="#0000ff" size="2">class</font>&nbsp;Script<br>
{<br>
&nbsp;&nbsp;&nbsp;&nbsp;[STAThread]<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">static</font>&nbsp;<font color="#0000ff" size="2">public</font>&nbsp;<font color="#0000ff" size="2">void</font>&nbsp;Main(<font color="#0000ff" size="2">string</font>[]&nbsp;args)<br>
&nbsp;&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SysInfoClass&nbsp;sysInfo&nbsp;=&nbsp;<font color="#0000ff" size="2">new</font>&nbsp;SysInfoClass();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">switch</font>&nbsp;(sysInfo.ACStatus)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">case</font>&nbsp;0:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(<font color="#800000" size="2">"Not&nbsp;using&nbsp;AC&nbsp;power"</font>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">break</font>;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">case</font>&nbsp;1:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(<font color="#800000" size="2">"Using&nbsp;AC&nbsp;power"</font>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">break</font>;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">default</font>:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(<font color="#800000" size="2">"Unknown&nbsp;AC&nbsp;power&nbsp;status"</font>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">break</font>;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">if</font>&nbsp;(sysInfo.BatteryLifePercent&nbsp;!=&nbsp;255)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(<font color="#800000" size="2">"Battery&nbsp;life&nbsp;"</font>+sysInfo.BatteryLifePercent+<font color="#800000" size="2">"%"</font>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">else</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(<font color="#800000" size="2">"Battery&nbsp;charge&nbsp;status&nbsp;not&nbsp;known"</font>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;}<br>
}</font></td>
    </tr>
  </tbody>
</table>
<p>Note that the COM server in this case does not require any
registration. This is because it is always present and registered on
any&nbsp;Windows with VB controls installed. And this is the reason
why
you can access it by
ProgID (<span style="color: rgb(51, 0, 153);">SYSINFO.SysInfo.1</span>
in <a href="Tutorial/interop/SysInfo1.cs"><span style="font-style: italic;">SysInfo1.cs</span></a>).&nbsp;</p>
<p>Alternativelly you can specify the COM server by it's
GUID (or TypeLibrary GUID):&nbsp;</p>
<p><span style="color: rgb(51, 0, 153);">//css_prescript
com({6FBA474B-43AC-11CE-9A0E-00AA0062BB4C}, SisInfoLib);</span></p>
<p>If you do not have VB controls installed the use of the
GUID/ProgID can be illustrated by the&nbsp;<a style="font-style: italic;" href="Tutorial/interop/msxml.cs">msxml.cs</a>
sample which utilises MS XML parser COM server (<span style="font-style: italic;">%SystemRoot%\system32\msxml2.dll</span>).</p>
<p>Another example of specifying the COM object by it's ProgID is the&nbsp;<a style="font-style: italic;" href="Tutorial/interop/setEV.cs">setEv.cs</a>
script. It illustrates how to use VBScript style programming to permanently set environment variable.
</p>
<p>Remember that the pre- and post-execution scripts are not executed
when the main script is opened for debugging in the VS2003 (this does
not apply for VS2005) .
</p>
<br>
<span style="font-style: italic; font-weight: bold;">Importing
type library manually<br>
</span>Download tutorial source code:&nbsp;<a href="Tutorial/interop/interop.zip"><span style="font-style: italic;">interop.zip</span></a>&nbsp;
<ol>
  <li>Extract<span style="font-style: italic;">
ATLSimpleServ.dll</span>&nbsp;from the zip file.<br>
This file contains implementation of <span style="color: rgb(51, 0, 153);">CTestObjClass</span>
CoClass. This class
has only one method <span style="color: rgb(51, 0, 153);">Test()</span>
that pops up the message
box.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</li>
  <li>Open command prompt. Make sure current directory is the
directory where Interop.cs is located.&nbsp;</li>
  <li>Register the COM server form command prompt:<br>
    <div class="syntax">regsvr32 ATLSimpleServ.dll</div>
  </li>
  <li>Create interop assembly (<span style="font-style: italic;">SimpleServ.dll</span>)
with Type Library Importer:<br>
    <div class="syntax">tlbimp ATLSimpleServ.dll
/out:SimpleServ.dll</div>
  </li>
  <li>Create file <span style="font-style: italic;">Interop.cs</span>
that contains the following code:&nbsp;<br>
    <table style="width: 710px; text-align: left;" border="1" cellpadding="2" cellspacing="2">
      <tbody>
        <tr>
          <td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font face="Courier New" size="2"><font color="#0000ff" size="2">using</font>&nbsp;System;<br>
          <font color="#0000ff" size="2">using</font>&nbsp;SimpleServ;<br>
          <br>
          <font color="#0000ff" size="2">class</font>&nbsp;Test<br>
{<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">public</font>&nbsp;<font color="#0000ff" size="2">static</font>&nbsp;<font color="#0000ff" size="2">void</font>&nbsp;Main(<font color="#0000ff" size="2">string</font>[]&nbsp;args)<br>
&nbsp;&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CTestObjClass&nbsp;COMObject&nbsp;=&nbsp;<font color="#0000ff" size="2">new</font>&nbsp;CTestObjClass();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;COMObject.Test();<br>
&nbsp;&nbsp;&nbsp;&nbsp;}<br>
}</font></td>
        </tr>
      </tbody>
    </table>
    <p>The script engine dynamically identifies <span style="font-style: italic;">SimpleServ.dll</span>
referenced from the code (<span style="color: rgb(51, 0, 153);">using
SimpleServ;</span>) and&nbsp;loads it at
run-time.&nbsp;Alternatively <span style="font-style: italic;">SimpleServ.dll</span>
can be loaded with <span style="color: rgb(51, 0, 153);">//css_reference</span>
directive:&nbsp;</p>
    <table style="width: 710px; text-align: left;" border="1" cellpadding="2" cellspacing="2">
      <tbody>
        <tr>
          <td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font face="Courier New" size="2"><font color="#008000" size="2">//css_reference&nbsp;SimpleServ.dll;</font></font><font face="Courier New" size="2"><font color="#0000ff" size="2"><br>
using</font>&nbsp;System&nbsp;;<br>
          <br>
          <font color="#0000ff" size="2">class</font>&nbsp;Test<br>
...</font></td>
        </tr>
      </tbody>
    </table>
    <pre class="code"></pre>
  </li>
  <li>Execute the following command in command prompt:<br>
    <div class="syntax">cscs interop </div>
  </li>
</ol>
<br>
<h4 class="dtH4">Code discussion</h4>
<p>The difference between the <span style="font-style: italic;">"Manual
importing"</span> and <span style="font-style: italic;">"Single-line
COM access"</span> samples is in the&nbsp;presence of <span style="color: rgb(51, 0, 153);">//css_prescript</span>
directive. The rest of the code is the same.</p>
<p>Note thet the script engine dynamically identifies <span style="font-style: italic;">SimpleServ.dll</span>
referenced from the code and dynamically loads it at the
run-time.&nbsp;&nbsp;Alternatively <span style="font-style: italic;">SimpleServ.dll</span> can
be loaded with the <span style="color: rgb(51, 0, 153);">//css_reference</span>
directive:&nbsp;</p>
<table style="width: 710px; text-align: left;" border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td style="white-space: nowrap; background-color: rgb(255, 255, 204);">
      <font face="Courier New" size="2"><font color="#008000" size="2">//css_prescript&nbsp;com(ATLSimpleServ.dll,&nbsp;SimpleServ,&nbsp;/r);</font></font><br>
      <font face="Courier New" size="2"><font color="#008000" size="2">//css_reference&nbsp;SimpleServ.dll;</font></font><br>
      <font face="Courier New" size="2"><font color="#0000ff" size="2">using</font>&nbsp;System&nbsp;;<br>
      <br>
      <font color="#0000ff" size="2">class</font>&nbsp;Test<br>
{<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">public</font>&nbsp;<font color="#0000ff" size="2">static</font>&nbsp;<font color="#0000ff" size="2">void</font>&nbsp;Main(<font color="#0000ff" size="2">string</font>[]&nbsp;args)<br>
&nbsp;&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SimpleServ.CTestObjClass&nbsp;COMObject&nbsp;=&nbsp;<font color="#0000ff" size="2">new</font>&nbsp;SimpleServ.CTestObjClass();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;COMObject.Test();<br>
&nbsp;&nbsp;&nbsp;&nbsp;}<br>
}</font></td>
    </tr>
  </tbody>
</table>
<h4 class="dtH4"><br>
<br>
See Also<br>
</h4>
<p><a href="Extending_scripting_system.html">
Extending the scripting system</a> |<span style="font-style: italic;">&nbsp;</span><a href="pre_post_scripts.html">Pre- and Post-execution scripts</a><span style="font-style: italic;"></span><br>
</p>
<br>
<object type="application/x-oleobject" classid="clsid:1e2a7bd0-dab9-11d0-b93a-00c04fc99f9e" viewastext="true" style="display: none;"> <param name="Keyword" value="COM tutorial">
</object>
</div>
</body></html>

